package com.tiancheng.trade.order.util;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

import java.util.Optional;

/**
 * @Auther: ld
 * @Date: 2024/2/28 16:22
 * @Param ${tags}
 * @Description:
 */
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ObjectConvertUtil {

	/** 转换对象
	 *
	 * @param resource
	 * @param target
	 * @param ignores
	 * @return
	 */
	public static <T> Optional<T> convert(Object resource, Class<T> target, String... ignores) {

		try {
			T t = target.newInstance();
			if (resource != null) {
				BeanUtils.copyProperties(resource, t, ignores);
			}
			return Optional.of(t);
		} catch (Exception e) {
			log.error(e.getMessage());
			return Optional.empty();
		}
	}

	/** 转换对象
	 *
	 * @param resource
	 * @param target
	 * @param clazz
	 * @param ignores
	 * @return
	 */
	public static <T> Optional<T> convert(Object resource, Object target, Class<T> clazz, String... ignores) {

		try {
			if (resource != null) {
				BeanUtils.copyProperties(resource, target, ignores);
			}
			return Optional.of((T) target);
		} catch (Exception e) {
			log.error(e.getMessage());
			return Optional.empty();
		}
	}
}
